﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using P2P_Backup.tools.io;
using System.IO;

namespace P2P_Backup.connections
{
    public class FileConnectionArgs : EventArgs
    {
        public FileConnectionArgs(archive.File file, double progress, double speed)
        {
            this.file = file;
            this.progress = progress;
            this.speed = speed;
        }

        private archive.File file;
        private double progress;
        private double speed;

        public archive.File File
        {
            get { return file; }
        }
        public double Progress
        {
            get { return progress; }
        }
        public double Speed
        {
            get { return speed; }
        }
    }
    
    public class LocalFileConnection
    {        
        public IPEndPoint EndPoint;
        public archive.File file;
        public ProgressReporter Progress;
        private TcpListener tcpListener;
        private TcpClient tcpClient;

        private StreamCopy streamCopy;
        private Thread fileSender;

        public LocalFileConnection(archive.File file)
        {
            this.file = file;
            tcpListener = new TcpListener(IPAddress.Any, 0);
            tcpListener.Start();
            streamCopy = new StreamCopy();
            Progress = new ProgressReporter(this.file, streamCopy);

            this.EndPoint = (IPEndPoint)tcpListener.LocalEndpoint;
            tcpListener.BeginAcceptTcpClient(new AsyncCallback(ReceiveTcp), EndPoint);

            
            
            if(file is archive.ArchivedFile)
                P2PBackup.Instance.LocalRestoreFileConnections.Add(this);
            else 
                P2PBackup.Instance.LocalBackupFileConnections.Add(this);
        }
        private void ReceiveTcp(IAsyncResult ar)
        {
            tcpClient = tcpListener.EndAcceptTcpClient(ar);
            IPEndPoint endPoint = (IPEndPoint)tcpClient.Client.RemoteEndPoint;

            Console.WriteLine("{0} Connected to recieve : {1}", endPoint,file.Name());

            fileSender = new Thread(sendFile);
            fileSender.IsBackground = true;
            fileSender.Start();

            tcpListener.Stop();
        }
        private void sendFile()
        {
            Console.WriteLine("Sending file : {0}", file.Name());            
            Progress.Begin();
            Stream input = file.GetReadableStream(); 
            streamCopy.CopyStream(input, tcpClient.GetStream());
            input.Close();
            if (file.Status != archive.TransferStatus.Failed)
            {
                Console.WriteLine("{0} was Succesfully send", file.Name());
                Progress.Done();
            }
            tcpClient.Close();
            P2PBackup.Instance.Save();
        }
    }
}
